<?php

namespace app\admin\controller;

use app\common\model\QbankName;
use app\common\model\QbankType;
use think\Controller;
use think\Loader;
use think\Request;
use app\common\model\ExamType as ExamSetting;

class ExamType extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function lst()
    {
        $artres = ExamSetting::all();
        $num = count($artres);
        $this->assign('artres', $artres);
        $this->assign('num', $num);
        return view('/examType/lst');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add()
    {
        //
        if (request()->isPost()) {
//            return dump(Request::instance()->param());
            $data = Request::instance()->post();
            $data['uid'] = Request::instance()->session('id');
//            dump($data);
            $type = new ExamSetting();
            $type->qname = $data['qname'];
            $type->qbank = $data['qbank_id'];
            $setting['time'] = $data['time'];
            for ($q = 1; $q < 6; $q++) {
                $setting['questions']['q' . $q] = $data['questions_q' . $q];
                $setting['score']['q' . $q] = $data['score_q' . $q];
            }
            $type->setting = json_encode($setting);
            $save = $type->save();
            if ($save) {
                $this->success('修改文章成功！');
            } else {
                $this->error('修改文章失败！');
            }
            return;
        }

        if ($this->request->isGet()) {


            $cate = new QbankName();
            $bankList = $cate->bankList();
            $this->assign('bankList', $bankList);

            //获取题型种类
            $q_type = new QbankType();
            $typeList = $q_type->typeList();
            $this->assign('typeList', $typeList);

            return view('/examType/add');
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit(int $id)
    {
        //
        if (request()->isPost()) {
            $data = Request::instance()->post();
//            $data['time'] = time();
            $data['uid'] = Request::instance()->session('id');
//            dump($data);
            $type = \app\common\model\ExamType::get($id);
            $type->qname = $data['qname'];
            $type->qbank = $data['qbank_id'];
            $setting['time'] = $data['time'];
            for ($q = 1; $q < 6; $q++) {
                $setting['questions']['q' . $q] = $data['questions_q' . $q];
                $setting['score']['q' . $q] = $data['score_q' . $q];
            }
            $type->setting = json_encode($setting);
            $save = $type->save();
            if ($save) {
                $this->success('修改文章成功！');
            } else {
                $this->error('修改文章失败！');
            }
            return;
        }

        if ($this->request->isGet()) {

            if (!is_numeric($id)) {
                return;
            }
            $q_bank = new QbankName();
            $bankList = $q_bank->bankList();
            $this->assign('bankList', $bankList);

//获取题型种类
            $q_type = new QbankType();
            $typeList = $q_type->typeList();
            $this->assign('typeList', $typeList);

            $ExamSetting = ExamSetting::get($id);
            $this->assign('ExamSetting', $ExamSetting);
            return view('/examType/edit');
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public
    function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public
    function delete()
    {

        if (ExamSetting::destroy(input('id'))) {
            $this->success('删除文章成功！');
        } else {
            $this->error('删除文章失败！');
        }

    }
}
